<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="APPKEY" prop="appkey">
        <el-input v-model="dataForm.appkey" placeholder="APPKEY"></el-input>
      </el-form-item>
      <el-form-item label="所属机构" prop="dtName">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择所属分销商"
        ></el-input>
      </el-form-item>
      <el-form-item label="secret" prop="secret">
        <el-input v-model="dataForm.secret" placeholder="secret"></el-input>
      </el-form-item>
      <el-form-item label="sessionkey" prop="sessionkey">
        <el-input v-model="dataForm.sessionkey" placeholder="sessionkey"></el-input>
      </el-form-item>
      <el-form-item label="aliCbAdd" prop="aliCbAdd">
        <el-input v-model="dataForm.aliCbAdd" placeholder="aliCbAdd"></el-input>
      </el-form-item>
      <el-form-item label="aliCbUser" prop="aliCbUser">
        <el-input v-model="dataForm.aliCbUser" placeholder="aliCbUser"></el-input>
      </el-form-item>
      <el-form-item label="aliCbUserPass" prop="aliCbUserPass">
        <el-input v-model="dataForm.aliCbUserPass" placeholder="aliCbUserPass"></el-input>
      </el-form-item>
      <el-form-item label="所属平台" prop="pfName">
        <el-select v-model="dataForm.blonPlantform" placeholder="请选择所属平台">
          <el-option
            v-for="item in pfs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            @change="pfChangeHandel"
          ></el-option>
        </el-select>
      </el-form-item>
      <template></template>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        appkey: '',
        dtId: '',
        dtName: '',
        secret: '',
        sessionkey: '',
        blonPlantform: 1,
        pfName: '',
        aliCbUserPass:"",
        aliCbAdd:"",
        aliCbUser:"",
      },
      pfs: [],
      dataRule: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        appkey: [
          { required: true, message: 'APPKEY不能为空', trigger: 'blur' }
        ],
        dtName: [
          { required: true, message: '所属分销商', trigger: 'blur' }
        ],
        secret: [
          { required: true, message: 'secret不能为空', trigger: 'blur' }
        ],
        sessionkey: [
          { required: true, message: 'sessionkey不能为空', trigger: 'blur' }
        ],
        blonPlantform: [
          { required: true, message: '所属平台不能为空', trigger: 'blur' }
        ]
      },
      dtList: [],
      dtListTreeProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/dt/storepf/query'),
        method: 'get'
      }).then(({ data }) => {
        this.dtList = treeDataTranslate(data.dts, 'id')
        this.pfs = data.spfs
        if (this.pfs.length > 0) {
          this.dataForm.blonPlantform = this.pfs[0].id
        }
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dt/store/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.store.name
                this.dataForm.appkey = data.store.appkey
                this.dataForm.dtId = data.store.dtId
                this.dataForm.dtName = data.store.dtName
                this.dataForm.secret = data.store.secret
                this.dataForm.sessionkey = data.store.sessionkey
                this.dataForm.blonPlantform = parseInt(data.store.blonPlantform)
                this.dataForm.pfName = data.store.pfName
                this.dataForm.aliCbAdd = data.store.aliCbAdd
                this.dataForm.aliCbUser = data.store.aliCbUser
                this.dataForm.aliCbUserPass = data.store.aliCbUserPass
              }
            })
          }
        })
      })
    },
    // 所属銷售平台
    pfChangeHandel(value) {
      this.dataForm.blonPlantform = value.id
      this.dataForm.pfName = value.label
    },
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.dtId = data.id
      this.dataForm.dtName = data.name
    },
    // 分销商设置当前选中节点
    dtListTreeSetCurrentNode() {
      // this.$refs.dtTree.setCurrentKey(this.dataForm.dtId)
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/dt/store/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'appkey': this.dataForm.appkey,
              'dtId': this.dataForm.dtId,
              'secret': this.dataForm.secret,
              'sessionkey': this.dataForm.sessionkey,
              'blonPlantform': this.dataForm.blonPlantform,
              'aliCbAdd': this.dataForm.aliCbAdd,
              'aliCbUser': this.dataForm.aliCbUser,
              'aliCbUserPass': this.dataForm.aliCbUserPass
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
